﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;
using System.Data.Entity.Validation;

namespace BLLA
{
    public partial class Order_Oper_T : BaseBLL<Model.Order_Oper_T>, IBLL.IOrder_Oper_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        IBLL.IBLLSession bl = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");

        /// <summary>
        /// 根据操作员权限获取对应商户权限
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string GetMer(int OperID, string YZM, int RETID)
        {
            try
            {
                WriteLog.WriteW("GetMer("+OperID+",\"\","+RETID+")");
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_Oper_T> ls = new List<Model.Order_Oper_T>();
                ls = base.GetListBy(" SELECT *   FROM  Order_Oper_T where OperID="+OperID);
             
             
                if (ls != null & ls.Count > 0)
                {
                    WriteLog.WriteW("GetMer  merid" + ls.First().MerID);
                    Model.Order_Oper_T info = ls.First();
                    List<Model.Order_Mer_T> list_info = new List<Model.Order_Mer_T>();
                    string[] mers = info.MerID.Split(',');
                    foreach (string item in mers)
                    {
                        List<Model.Order_Mer_T> list_tmp = bl.IOrder_Mer_TBLL.GetListBy(u => u.MerID == item);
                        Model.Order_Mer_T info_tmp = list_tmp.First();
                        if (info_tmp != null && list_tmp.Count > 0)
                        {
                            list_info.Add(info_tmp);
                        }
                    }

                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = list_info.Count;
                    result.rows = list_info;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("获取第三方信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 更新商户权限
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="json">更新信息</param>
        /// <param name="tag">操作标签（1：新增；2：修改；3：删除）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string SetMerOperID(int OperID, string json, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_Oper_T info = ParseClass<Model.Order_Oper_T>.StrDeserialize(json, RETID);
                List<Model.Order_Oper_T> ORList = base.GetListBy(u => u.OperID == OperID);
                string T = "";
                int ret = -1;
               int tag = ORList.Count > 0 ? 2 : 1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        T = "新增";
                        ret = base.Add(info);
                        break;
                    case 2:
                        T = "修改";
                        Model.Order_Oper_T tmp = ORList.First();
                        Help.FuZhi<Model.Order_Oper_T>.FZ(ref tmp, info);
                        ret = base.Modify(tmp);
                        break;
                    case 3:
                        T = "删除";
                        //ret = base.Del(info);
                        break;
                }

                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "更新商户权限设置";
                    Log.OperationText = T + "商户权限成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                WriteLog.WriteW("SetMerOperID失败" + dbEx.Message);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商户权限失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

    }
}
